package vn.com.epi.algorithm.knn;

import java.util.Random;

import vn.com.epi.algorithm.clustering.Data;

public class BruteForceTemplate {
	
	private int n;
	private int[] label;
	
	public BruteForceTemplate() {
		
	}
	
	public BruteForceTemplate(int[] lb) {
		label = new int[lb.length];
		for (int i = 0; i < label.length; i++)
			label[i] = lb[i];
		n = label.length;
	}	

	public int[] nearest(int index, int k) {		
		int[] r = new int[k];
		double[] val = new double[k];
		for (int i = 0; i < k; i++)
			val[i] = 0;
		for (int i = 0; i < n; i++)
			if (label[i] != index) {
				double sim = Data.distance(label[i], index);
				for (int j = 0; j < k; j++)
					if (val[j] < sim) {
						for (int t = k - 1; t > j; t--) {
							val[t] = val[t - 1];
							r[t] = r[t - 1];
						}
						val[j] = sim;
						r[j] = label[i];
						break;
					}
			}
		return r;
	}
	
	public void test(int n_) {
		Random rand = new Random(System.currentTimeMillis());
		new Data(n_); // create database
		for (int i = 0; i < n_; i++) {
			int k = 5 + rand.nextInt(10);
			boolean[] b = new boolean[30];
			for (int j = 0; j < 30; j++)
				b[j] = false;
			for (int j = 0; j < k; j++) {
				short r = (short)rand.nextInt(30);
				if (!b[r]) {
					short p = (short)(3 + rand.nextInt(10));
					Data.add(i, r, p);
					b[r] = true;
				}
			}
		}
		Data.initial();
		int[] label = new int[n_];
		for (int i = 0; i < n_; i++)
			label[i] = i;
		
	}
	
	public static void main(String[] args) {
		int n_ = 2000;
		Random rand = new Random(System.currentTimeMillis());
		new Data(n_); // create database
		for (int i = 0; i < n_; i++) {
			int k = 5 + rand.nextInt(10);
			boolean[] b = new boolean[30];
			for (int j = 0; j < 30; j++)
				b[j] = false;
			for (int j = 0; j < k; j++) {
				short r = (short)rand.nextInt(30);
				if (!b[r]) {
					short p = (short)(3 + rand.nextInt(10));
					Data.add(i, r, p);
					b[r] = true;
				}
			}
		}
		Data.initial();
		long start = System.currentTimeMillis();
		int[] label = new int[n_];
		for (int i = 0; i < n_; i++)
			label[i] = i;		
		BruteForceTemplate br = new BruteForceTemplate(label);
		for (int i = 0; i < n_; i++) {
			int[] nears = br.nearest(i, 3);
			String s = "";
			for (int j = 0; j < 3; j++)
				s += nears[j] + ",";
			System.out.println(i + ":\t" + s);
		}
		long end = System.currentTimeMillis();
		System.out.println("Total time used: " + (end - start) + " ms");	
		
		
//		new Data(6);
//		Data.add(0, (short)0, (short)1);
//		Data.add(0, (short)1, (short)1);
//		
//		Data.add(1, (short)0, (short)4);
//		Data.add(1, (short)1, (short)8);
//		
//		Data.add(2, (short)0, (short)2);
//		Data.add(2, (short)1, (short)5);
//		
//		Data.add(3, (short)0, (short)7);
//		Data.add(3, (short)1, (short)10);
//		
//		Data.add(4, (short)0, (short)4);
//		Data.add(4, (short)1, (short)10);
//		
//		Data.add(5, (short)0, (short)4);
//		Data.add(5, (short)1, (short)10);
//		
//		Data.initial();
//		int[] label = new int[] {0, 1, 2, 3, 4, 5};
//		BruteForce br = new BruteForce(label);
//		int[] res = br.nearest(4, 3);
//		for (int i = 0; i < 3; i++)
//			System.out.print(res[i] + "   ");
//		System.out.println();

		
	}
}
